import React, {useEffect, useState} from 'react'
import {Route, Switch, Redirect} from 'react-router-dom'
import {connect} from 'react-redux'
import {Spin} from 'antd'
import axios from 'axios'
//首页
import Home from '../../views/NewsSandBox/home/Home'
// 用户列表
import UserList from '../../views/NewsSandBox/user-manage/UserList'
// 权限管理
import RightList from '../../views/NewsSandBox/righe-manage/RightList'
import RoleList from '../../views/NewsSandBox/righe-manage/RoleList'
// 新闻管理
import NewsAdd from '../../views/NewsSandBox/news-manage/NewsAdd'
import NewsDraft from '../../views/NewsSandBox/news-manage/Newsdraft'
import NewsCategory from '../../views/NewsSandBox/news-manage/NewsCategory'
import NewPreview from '../../views/NewsSandBox/news-manage/NewPreview'
import NewUpdata from '../../views/NewsSandBox/news-manage/NewUpdata'
// 审核管理
import Audit from '../../views/NewsSandBox/audit-manage/Audit'
import AuditList from '../../views/NewsSandBox/audit-manage/AuditList'
// 发布管理
import Unpublished from '../../views/NewsSandBox/publish-manage/Unpublished'
import Published from '../../views/NewsSandBox/publish-manage/Published'
import Sunset from '../../views/NewsSandBox/publish-manage/Sunset'

import NoPermission from '../../views/NewsSandBox/nopermission/NoPermission'

function NewsRoute(props) {
    const [BackRoudeList, setBackRoudeList] = useState([])
    const {role:{rights}} = JSON.parse(localStorage.getItem('token'))
    useEffect(() => {
        Promise.all([
            axios.get('/rights'),
            axios.get('/children')
        ]).then(res=>{
            setBackRoudeList([...res[0].data, ...res[1].data])
        })
    }, []);
    //路由结构拆分
    const LocalRouterMap = {
        "/home": Home ,
        "/user-manage/list": UserList,
        "/right-manage/role/list": RoleList,
        "/right-manage/right/list": RightList,
        "/news-manage/add": NewsAdd ,
        "/news-manage/draft": NewsDraft ,
        "/news-manage/category": NewsCategory,
        "/news-manage/preview/:id": NewPreview,
        "/news-manage/update/:id": NewUpdata,
        "/audit-manage/audit": Audit ,
        "/audit-manage/list": AuditList,
        "/publish-manage/unpublished": Unpublished,
        "/publish-manage/published" : Published,
        "/publish-manage/sunset": Sunset
    }
    const checkRoute = (item) => {
        return LocalRouterMap[item.key] && (item.pagepermisson||item.routepermisson)
    }
    const checkUserPermisson = (item) => {
        return rights.includes(item.key)
    }
    return (
        <Spin tip="加载中......" spinning={props.isLogin}>
            <Switch>
                {
                    BackRoudeList.map(item=>{
                        if (checkRoute(item) && checkUserPermisson(item)) {
                            return <Route path={item.key} key={item.key} component={LocalRouterMap[item.key]}/>
                        }
                        
                    })
                }
                <Redirect from='/' to="/home" exact/>
                <Route path="*" component = {NoPermission}/>
            </Switch>
        </Spin>
    )
}
const mapStateToProps = (state)=> {
    const {Login:{isLogin}} = state
    return {
        isLogin
    }
}
export default connect(mapStateToProps)(NewsRoute)